﻿// TestSeqEditor.cpp: 实现文件
//

#include "stdafx.h"
#include "TestSeqEditor.h"
#include "afxdialogex.h"
#include "resource.h"
#include "McuUARTCmdManger.h"
#include "myUtil.h"
#include <locale.h>
#include "chineseString.h"

// TestSeqEditor 对话框

IMPLEMENT_DYNAMIC(TestSeqEditor, CDialogEx)

TestSeqEditor::TestSeqEditor(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_EDIT_TEST_SEQ, pParent)
{
	setString = "";
	port1 = 1;
	port2 = 1;
	rangeMax = "2k";
	minlimit = "0";
	maxlimit = "20M";
}

TestSeqEditor::~TestSeqEditor()
{
}

void TestSeqEditor::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_PORT1, mPort1Sel);
	DDX_Control(pDX, IDC_COMBO_PORT2, mPort2Sel);
	DDX_Control(pDX, IDC_EDIT_MIN, mEditMin);
	DDX_Control(pDX, IDC_EDIT_MAX, mEditMax);
	DDX_Control(pDX, IDC_COMBO_RANGE_SEL, mRangeSel);
	DDX_Control(pDX, IDC_COMBO_EXPECTMAX_2, mExpectMax2);
}


BOOL TestSeqEditor::OnInitDialog()
{
	wchar_t* old_locale = _wcsdup(_wsetlocale(LC_CTYPE, NULL));
	_wsetlocale(LC_CTYPE, _T("chs"));//设定
	CDialogEx::OnInitDialog();
	McuUARTCmdManger tmp;
	mExpectMax2.InsertString(0, L"0");
	mExpectMax2.InsertString(1, L"∞");
	mExpectMax2.SetCurSel(1);

	for (int i = 1; i <= rangeStrVec.size(); i++)
	{
		mRangeSel.InsertString(mRangeSel.GetCount(), rangeStrVec[i-1]);
	}
	//历史原因+regex原因 这里有这么奇怪的映射 10==>低阻仪 10M==>M500 99999M==>绝缘表
	//默认选择M500
	mRangeSel.SetCurSel(1);
	if (rangeMax == L"10")
		mRangeSel.SetCurSel(0);
	if (rangeMax == L"10M")
		mRangeSel.SetCurSel(1);
	if (rangeMax == L"99999M")
		mRangeSel.SetCurSel(2);

	
	for (int i = 1; i <= 51; ++i)
	{
		CString portNum;
		portNum.Format(L"%02d", i);
		CString combPortName = portNum;
		mPort1Sel.InsertString(mPort1Sel.GetCount(), combPortName);
	}
	mPort1Sel.SetCurSel(port1-1);

	for (int i = 1; i <= 51; ++i)
	{
		CString portNum;
		portNum.Format(L"%02d", i);
		CString combPortName = portNum;
		mPort2Sel.InsertString(mPort2Sel.GetCount(), combPortName);
	}
	mPort2Sel.SetCurSel(port2-1);
	mEditMin.SetWindowTextW(minlimit);
	//mEditMax.SetWindowTextW(downlimit);
	mExpectMax2.SetWindowTextW(maxlimit);

	mEditMax.ShowWindow(FALSE);


	_wsetlocale(LC_CTYPE, old_locale);
	free(old_locale);//还原区域设定
	return TRUE;  // return TRUE  unless you set the focus to a control
}


BEGIN_MESSAGE_MAP(TestSeqEditor, CDialogEx)
	ON_BN_CLICKED(IDOK, &TestSeqEditor::OnBnClickedOk)
	ON_CBN_EDITCHANGE(IDC_COMBO_EXPECTMAX_2, &TestSeqEditor::OnCbnEditchangeComboExpectmax2)
	ON_CBN_SELCHANGE(IDC_COMBO_EXPECTMAX_2, &TestSeqEditor::OnCbnSelchangeComboExpectmax2)
	ON_WM_TIMER()
END_MESSAGE_MAP()


// TestSeqEditor 消息处理程序


void TestSeqEditor::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	CString tmp = Seq_Space;
	CString port1, port2, expectMin, expectMax, rangeSel;
	mPort1Sel.GetWindowTextW(port1);
	mPort2Sel.GetWindowTextW(port2);
	mEditMin.GetWindowTextW(expectMin);
	//mEditMax.GetWindowTextW(expectMax);
	mExpectMax2.GetWindowTextW(expectMax);
	mRangeSel.GetWindowTextW(rangeSel);
	setString = tmp + port1 + Port1_Space + port2 + Port2_Space
		+ myUtil::formatSeqStrMinMaxPart(expectMin) + ExpMin_Space
		+ myUtil::formatSeqStrMinMaxPart(expectMax) + ExpMax_Space 
		+ myUtil::formatRangePart(rangeSel);
	CDialogEx::OnOK();
}
//add event for mExpectMax2's changing

void TestSeqEditor::changeSelRange()
{
	//CString strExpectMax;
	//mExpectMax2.GetWindowTextW(strExpectMax);
	//if (strExpectMax.Find(chi_wu_qiong) != -1)
	//	strExpectMax = L"99999M";
	//string tmp = CT2A(strExpectMax.GetBuffer());
	//float maxVal = myUtil::transToRealLimit(tmp);
	//if (maxVal <= 10.0)
	//{
	//	mRangeSel.SetCurSel(0);
	//}
	//else if (maxVal <= 10000000)
	//{
	//	mRangeSel.SetCurSel(1);
	//}
	//else
	//{
	//	mRangeSel.SetCurSel(2);
	//}
}

void TestSeqEditor::enhanceChangeSelRange()
{
	//changeSelRange();
	//chang expected max to wu_qiong, need some time to update it
	//SetTimer(11, 20, NULL);
}
void TestSeqEditor::OnCbnEditchangeComboExpectmax2()
{
	enhanceChangeSelRange();
}


void TestSeqEditor::OnCbnSelchangeComboExpectmax2()
{
	enhanceChangeSelRange();
}


void TestSeqEditor::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	switch (nIDEvent)
	{
	case 11:
		KillTimer(11);
		changeSelRange();
		break;
	default:
		break;
	}
	CDialogEx::OnTimer(nIDEvent);
}
